package com.example.mallapi.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.mallapi.common.Result;
import com.example.mallapi.entity.Product;
import com.example.mallapi.qo.ProductQo;
import com.example.mallapi.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 产品表 前端控制器
 * </p>
 *
 * @author zhengpf
 * @since 2023-06-03
 */
@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {
    @Autowired
    private IProductService productService;

    @GetMapping("list")
    public Result list(ProductQo qo){
        IPage<Product> page = productService.page(qo);

        return Result.success("商品查询成功",page);
    }

    @PostMapping("upload")
    public Result upload(MultipartFile file,String imageType) {

        log.info("上传文件：{}",file);
        log.info("上传文件类型{}",imageType);

        String uploadPath = productService.uploads(file, imageType);
        return Result.success("图片上传成功",uploadPath);
    }

    //商品的添加
    @PostMapping("insert")
    public Result insert(@RequestBody Product product){
        productService.save(product);
        return Result.success("商品添加成功");
    }

    //数据回显
    @GetMapping("info/{id}")
    public Result info(@PathVariable Integer id){
        Product product = productService.getById(id);
        return Result.success("数据回显成功",product);
    }

    //修改商品信息
    @PutMapping("update")
    public Result update(@RequestBody Product product){
        productService.updateById(product);
        return Result.success("商品修改信息成功");
    }

    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable Integer id){
        productService.removeById(id);
        return Result.success("商品信息删除成功");
    }
}

